﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace RPGProject
{
    class Constants
    {
        private static Constants instance;
        private Dictionary<String, String> stringValues;
        private Dictionary<String, int> values;
        private Constants()
        {
            this.values = new Dictionary<String, int>();
            this.stringValues = new Dictionary<string, string>();
        }

        static void TMain(String[] args)
        {
            Constants c = Constants.GetInstance();
            Console.Write("");
        }

        public static Constants GetInstance()
        {
            if (Constants.instance == null)
            {
                Constants.instance = new Constants();

                Constants.instance.BuildDefault(); //set default values
                Constants.instance.Load("constants.txt"); //load in all values from the file... This way, any values not adressed in the file will have their default values
            }
            return Constants.instance;
        }

        public int this[String key]
        {
            get { return this.values[key]; }
        }


        private bool Load(String fileName)
        {
            // Check for file existence
            FileInfo fi = new FileInfo(fileName);
            if (!fi.Exists)
                return false;
            StreamReader sr = new StreamReader(fi.FullName);
            string st;
            string[] arr;
            // read through the file
            while (!sr.EndOfStream)
            {
                st = sr.ReadLine();
                // split the line from "Map.Size 1" => {"Map.Size", "1"}
                arr = st.Split(' ');
                // adds the value to the map
                int testValue;
                if (Int32.TryParse(arr[1], out testValue))
                {
                    this.values[arr[0]] = testValue;
                }
                else
                {
                    this.stringValues[arr[0]] = arr[1];
                }
            }
            return true;
        }

        private void BuildDefault()
        {
            this.values["Player.MaxHP"] = 400;
            this.values["Player.Range"] = -1;
            this.values["Player.MeleeDamage"] = 100;
            this.values["Player.RangedDamage"] = 50;
            this.values["Player.ActionPoints"] = 2;
            this.values["Ninja.MaxHP"] = 100;
            this.values["Ninja.StandardAmount"] = 3;
            this.values["Ninja.Range"] = 10;
            this.values["Ninja.MeleeDamage"] = 30;
            this.values["Ninja.RangedDamage"] = 15;
            this.values["Ninja.ActionPoints"] = 3;
            this.values["AccuracyFactor"] = 4;
            this.stringValues["StartingMap"] = "level1/map.txt";
        }

        public int GetInt(String p)
        {
            return this[p];
        }
        public string GetString(String p)
        {
            return this.stringValues[p];
        }
    }
}
